import 'dart:isolate';

import 'package:flutter/material.dart';
import 'package:ui_demo/market/stock.dart';
import 'package:ui_demo/util/print_util.dart';

import 'base/sp_util.dart';

Future<void> main() async {
  // await SpUtil.getInstance();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static const mTAG = "_MyHomePageState";

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
          child: Column(
        children: [
          TextButton(
              onPressed: () {
                // Stock stock = Stock("111", "222", "333");
                // SpUtil.putObject("key1", stock);
                ReceivePort mReceivePort = ReceivePort();
                SpUtil.getInstance().then(
                    (value) => LogUtils.d("SpUtil1：${value.hashCode}", mTAG));
                // SpUtil.getInstance1().then((value) => LogUtils.d("SpUtil2：${value.hashCode}", mTAG));
                Isolate.spawn((message) {
                  SpUtil.getInstance().then((value) {
                    LogUtils.d("SpUtil3", mTAG);
                    LogUtils.d("SpUtil31：${value.hashCode}", mTAG);
                    // Stock stock1 = Stock("444", "555", "666");
                    // LogUtils.d("build $stock1", mTAG);
                    // SpUtil.putObject("key1", stock1);
                    // Stock? stock = SpUtil.getObj<Stock>("key1", (v) {
                    //   LogUtils.d(v.toString());
                    //   return Stock.fromJson(v as Map<String, dynamic>);
                  });
                  // SpUtil.getInstance1().then((value) => LogUtils.d("SpUtil4：${value.hashCode}", mTAG));
                  // LogUtils.d(stock.toString());
                }, mReceivePort.sendPort);
              },
              child: Text("Save Data")),
          TextButton(
              onPressed: () {
                Stock? stock = SpUtil.getObj<Stock>("key1", (v) {
                  LogUtils.d(v.toString());
                  return Stock.fromJson(v as Map<String, dynamic>);
                });
                LogUtils.d(stock.toString());
              },
              child: Text("Read Data")),
          Offstage()
        ],
      )),
      // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
